var http = require('http');
var fs = require('fs');
 var async = require('async');
 var url = require('url');
 var info =require('debug')('log:info');
var path = require("path");
const routers = require('./src/routers')
const app =http.createServer(function (req, res) {
  info(__dirname);
  info(req.url);
  req.socket = app.socket;
  const u =url.parse(req.url);
  if(u.pathname == '/'){
	  res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
	  res.type = 'html';

	  fs.createReadStream('./dist/index.html').pipe(res);
  }
  else{
  	if(fs.existsSync('./dist/'+u.pathname)){
  		fs.createReadStream('./dist/'+u.pathname).pipe(res);
  	}
  	else{
      routers(req,res);
  		// res.writeHead(404, {'Content-Type': 'text/html;charset=utf-8'});
  		// res.type = 'html';
  		// res.end('Not found');
  	}
  }
  // res.end();
    // res.writeHead(200, {'Content-Type': 'text/plain'});
    // res.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
const io = require('socket.io').listen(app)
io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
  app.socket = socket;
});